package com.yaongteam;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class AndroidSample extends Activity {

	class Sample {
		Class<?> clazz;
		String title;
		Sample(Class<?> clazz, String title){
			this.clazz = clazz;
			this.title = clazz.getSimpleName() + " : " + title;
		}
	}

	Sample[][] arSample = {
			{
				new Sample(AndroidSample.class, "예제1-1"),
				new Sample(AndroidSample.class, "예제1-2")
			},
			{
				new Sample(AndroidSample.class, "예제2-1"),
				new Sample(AndroidSample.class, "예제2-2")				
			}
	};

	Spinner mainSpinner;
	ListView mainListView;
    ArrayAdapter<CharSequence> typeAdapter;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mainSpinner = (Spinner)findViewById(R.id.mainSpinner);
        mainSpinner.setPromptId(R.string.type_select);
        

        typeAdapter = ArrayAdapter.createFromResource(this, R.array.typeArray, android.R.layout.simple_spinner_item);
        //typeAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, R.array.typeArray);
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        mainSpinner.setAdapter(typeAdapter);
        
        mainSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(AndroidSample.this, typeAdapter.getItem(position), Toast.LENGTH_SHORT).show();
				ChangeType(position);
			}

			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
        
        mainListView = (ListView)findViewById(R.id.mainListView);
        
        ArrayAdapter<CharSequence> fruitsAdapter;
        fruitsAdapter = ArrayAdapter.createFromResource(this, R.array.fruits, android.R.layout.simple_list_item_single_choice);
        
        //mainListView.setAdapter(fruitsAdapter);
    }

    public void ChangeType(int position)
    {
    	ArrayList<String> typeTitleList = new ArrayList<String>();
    	for(Sample sample : arSample[position]){
    		typeTitleList.add(sample.title);
    	}
    	
    	ArrayAdapter<String> sampleAdapter;
    	sampleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, typeTitleList);
    	
    	mainListView.setAdapter(sampleAdapter);
    }
    
}